package com.sufen.seckill.utils;

import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

@Component
public class MD5UTIL {
    public static String md5(String s) {
        return DigestUtils.md5DigestAsHex(s.getBytes());
    }

    private static String salt = "1a2b3c4d";

    public static String inputPassToFromPass(String inputPass) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    public static String fromPassToDBPass(String fromPass, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + fromPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    public static String inputPassToDBPass(String inputPass, String salt) {
        String fromPass = inputPassToFromPass(inputPass);
        String DBPass = fromPassToDBPass(fromPass, "1a2b3c4d");
        return DBPass;
    }

    public static void main(String[] args) {
        System.out.println(inputPassToFromPass("123456"));
        System.out.println(fromPassToDBPass(inputPassToFromPass("123456"), "1a2b3c4d"));
        System.out.println(inputPassToDBPass("123456", "1a2b3c4d"));
    }
}
